﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sispe.Entities;
using System.Data;

namespace Sispe.Repository
{
    public class VacacionesRepository: MasterRepository, IVacacionesRepository
    {
        public List<VACACIONE> GetAllFromVacaciones()
        {
            var query = from v in context.VACACIONES.Include("CONTRATO")
                        //where v.IdContrato.Equals(9)
                        select v;

            return query.ToList();
        }

        public VACACIONE GetFromVacacionesById(int id)
        {
            var query = from ts in context.VACACIONES
                        where ts.IdVacaciones.Equals(id)
                        select ts;
            return query.SingleOrDefault();
        }

        public void AddVacaciones(VACACIONE vacaciones)
        {
            vacaciones.CONTRATO = null;
            
            context.VACACIONES.Add(vacaciones);
            try
            {
                context.SaveChanges();
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException e)
            {

                var outputLines = new List<string>();
                foreach (var eve in e.EntityValidationErrors)
                {
                    outputLines.Add(string.Format(
                        "{0}: Entity of type \"{1}\" in state \"{2}\" has the following validation errors:",
                        DateTime.Now, eve.Entry.Entity.GetType().Name, eve.Entry.State));
                    foreach (var ve in eve.ValidationErrors)
                    {
                        outputLines.Add(string.Format(
                            "- Property: \"{0}\", Error: \"{1}\"",
                            ve.PropertyName, ve.ErrorMessage));
                    }
                }
                if (System.IO.File.Exists(@"D:\errors.txt"))
                {
                    System.IO.File.Delete(@"D:\errors.txt");
                    System.IO.File.AppendAllLines(@"d:\errors.txt", outputLines);
                }
                else
                {
                    System.IO.File.AppendAllLines(@"d:\errors.txt", outputLines);
                }
                throw;
            }
        }

        public void UpdateVacaciones(VACACIONE vacaciones)
        {
            vacaciones.CONTRATO = null;
            context.Entry(vacaciones).State = System.Data.EntityState.Modified;
            try
            {
                context.SaveChanges();
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException e)
            {

                var outputLines = new List<string>();
                foreach (var eve in e.EntityValidationErrors)
                {
                    outputLines.Add(string.Format(
                        "{0}: Entity of type \"{1}\" in state \"{2}\" has the following validation errors:",
                        DateTime.Now, eve.Entry.Entity.GetType().Name, eve.Entry.State));
                    foreach (var ve in eve.ValidationErrors)
                    {
                        outputLines.Add(string.Format(
                            "- Property: \"{0}\", Error: \"{1}\"",
                            ve.PropertyName, ve.ErrorMessage));
                    }
                }
                if (System.IO.File.Exists(@"D:\errors.txt"))
                {
                    System.IO.File.Delete(@"D:\errors.txt");
                    System.IO.File.AppendAllLines(@"d:\errors.txt", outputLines);
                }
                else
                {
                    System.IO.File.AppendAllLines(@"d:\errors.txt", outputLines);
                }
                throw;
            }
        }

        public void RemoveVacaciones(int id)
        {
            var existe = context.VACACIONES.Find(id);

            if (existe != null)
            {
                context.VACACIONES.Remove(existe);
                context.SaveChanges();
            }
        }

        public List<VACACIONE> GetAllFromVacacionesByIdCOntrato(int id)
        {
            var query = from v in context.VACACIONES
                        where v.IdContrato.Equals(id)
                        select v;
            return query.ToList();
        }


        public List<VACACIONE> GetAllVacacionesByContrato(string contrato)
        {
            if (contrato != null && contrato != string.Empty && contrato != "")
            {
                var query = from c in context.VACACIONES
                                 .Include("CONTRATO.TRABAJADOR.PERSONA")

                            where (c.CONTRATO.TRABAJADOR.PERSONA.Dni.ToUpper().Contains(contrato))
                            select c;
                return query.ToList();
            }
            else
            {
                var query = from c in context.VACACIONES
                                 .Include("CONTRATO.TRABAJADOR.PERSONA")
                            select c;
                return query.ToList();
            }
        }
    }
}
